<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="groupName">
        <el-input v-model="queryParams.groupName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ehr:customer:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ehr:customer:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['ehr:customer:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ehr:customer:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户编号" align="center" prop="partyId" width="80" />
      <el-table-column label="客户名称" align="center" prop="groupName" />
      <el-table-column label="办公地址" align="center" prop="officeSiteName" />
      <el-table-column label="手机号" align="center" prop="mobilePhone" width="120" />
      <el-table-column label="电话" align="center" prop="telephone" width="120" />
      <el-table-column label="邮箱" align="center" prop="mail" width="120" />
      <el-table-column label="状态" align="center" prop="statusId" width="80" />
      <!-- <el-table-column label="备注" align="center" prop="comments" /> -->
      <!-- <el-table-column label="更新时间" align="center" prop="lastUpdatedStamp" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdatedStamp, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleDetail(scope.row)"
            v-hasPermi="['ehr:customer:detail']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ehr:customer:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ehr:customer:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-document" @click="handleQRCodeGenerato(scope.row)"
            v-hasPermi="['ehr:customer:detail']">生成二维码</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />




    <!-- 添加或修改客户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-bind:disabled=detailFlag>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="groupName">
              <el-input v-model="form.groupName" placeholder="请输入客户名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="客户类型" prop="groupTypeId">
              <el-select v-model="form.groupTypeId" placeholder="请选择客户类型">
                <el-option v-for="dict in groupTypeIdOptions" :key="dict.enumCode" :label="dict.description"
                  :value="dict.enumCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="客户行业" prop="industryEnumId">
              <el-select v-model="form.industryEnumId" placeholder="请选择客户行业">
                <el-option v-for="dict in industryEnumIdOptions" :key="dict.enumCode" :label="dict.description"
                  :value="dict.enumCode"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="办公地址" prop="officeSiteName">
              <el-input v-model="form.officeSiteName" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号" prop="mobilePhone">
              <el-input v-model="form.mobilePhone" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="telephone">
              <el-input v-model="form.telephone" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="邮箱" prop="mail">
              <el-input v-model="form.mail" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="备注" prop="comments">
              <el-input v-model="form.comments" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="statusId">
              <el-input v-model="form.statusId" placeholder="请输入状态" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="editFlag" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <el-dialog title="二维码生成" :visible.sync="openQCode" width="15%" style="text-align: center" >
      <canvas ref="canvas"></canvas>
    </el-dialog>
  </div>
</template>

<script>
import { listCustomer, getCustomer, delCustomer, addCustomer, updateCustomer } from "@/api/ehr/customer";
// import QRCode from 'qrcode'
export default {
  name: "Customer",
  data() {
    return {
      openQCode:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户管理表格数据
      customerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 客户类型业务字典
      groupTypeIdOptions: [],
      // 客户行业业务字典
      industryEnumIdOptions: [],
      // 操作标识,用于区分是新增还是修改
      updateFlag: false,
      // 操作标识,用于区分是详情
      detailFlag: false,
      // 操作标识,用于区分编辑状态
      editFlag: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "", // 排序字段
        isAsc: "", // 升序或降序 大写的 ASC或DESC,为空代表升序
        groupName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        groupName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        officeSiteName: [
          { required: true, message: "办公地址不能为空", trigger: "blur" }
        ],
      }
    };
  },


  created() {
    // 客户类型业务字典
    this.getEnumDicts("CUSTOMER_TYPE").then((response) => {
      this.groupTypeIdOptions = response.data;
    });
    // 客户行业业务字典
    this.getEnumDicts("CUSTOMER_INDUSTRY").then((response) => {
      this.industryEnumIdOptions = response.data;
    });
    this.getList();
  },
  methods: {
    groupTypeIdFormat(row, column) {
      let val = null;
      this.groupTypeIdOptions.forEach((item, index) => {
        if (row.groupTypeId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    industryEnumIdFormat(row, column) {
      let val = null;
      this.industryEnumIdOptions.forEach((item, index) => {
        if (row.industryEnumId === item.enumCode) {
          val = item.description;
        }
      });
      return val;
    },
    /** 查询客户管理列表 */
    getList() {
      this.loading = true;
      listCustomer(this.queryParams).then(response => {
        this.customerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        partyId: null,
        groupName: null,
        groupTypeId: null,
        industryEnumId: null,
        statusId: null,
        officeSiteName: null,
        numEmployees: null,
        tickerSymbol: null,
        comments: null,
        logoImageUrl: null,
        mobilePhone: null,
        telephone: null,
        mail: null,
        lastUpdatedStamp: null,
        lastUpdatedTxStamp: null,
        createdStamp: null,
        createdTxStamp: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.partyId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editFlag = true,
        this.updateFlag = false;
      this.detailFlag = false;
      this.reset();
      this.open = true;
      this.title = "添加客户管理";
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.editFlag = false,
        this.updateFlag = false;
      this.detailFlag = true;
      this.reset();
      const partyId = row.partyId || this.ids
      getCustomer(partyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "客户管理详情";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editFlag = true;
      this.updateFlag = true;
      this.detailFlag = false;
      this.reset();
      const partyId = row.partyId || this.ids
      getCustomer(partyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.partyId != null && this.updateFlag) {
            updateCustomer(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCustomer(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const partyIds = row.partyId || this.ids;
      this.$modal.confirm('是否确认删除客户管理编号为"' + partyIds + '"的数据项？').then(function () {
        return delCustomer(partyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ehr/customer/export', {
        ...this.queryParams
      }, `customer_${new Date().getTime()}.xlsx`)
    },
    /** 生成二维码操作 */
    handleQRCodeGenerato(row) {
      this.openQCode=true;
      this.qrcode (row);
    },
    qrcode (row) {
      let opts = {
        errorCorrectionLevel: 'H',
        type: 'image/png',
        quality: 0.3,
        width: 165,
        height: 165,
        text: 'xxx',
        color: {
          dark: '#333333',
          light: '#fff'
        }
      }
      // QRCode.toCanvas(this.$refs.canvas, JSON.parse(row), opts)
    }

  }
};
</script>
